//1.引入模块
var conn = require("./baseDb.js")

//2.向外暴露对象
module.exports = {
    getCommentsData(callback){ // 获取当前评论页面的数据
        // 需要查询 id author created status content title数据
        //写sql邮件查询,联表查询comments posts表格
        var sql = "select c.id,c.author,c.created,c.status,c.content,p.title from comments as c left join posts as p on c.post_id=p.id order by c.id limit 10 ; select count(*) as totalCount from comments"
        conn.query(sql,(err,result)=>{
            if(err) return callback(err)
            // console.log(result)
            callback(null,result)
        })
    },
    getCommentsDataByPage(page,callback){ // 通过页码获取对应的数据
        var pageSize = 10;  // 设置每页显示的条数
        var offset = (page-1)*pageSize; // 设置索引数据的公式
        //写sql语句查询,根据页码查询对应的索引的10条数据
        // 需要查询 id author created status content title数据
        var sql = "select c.id,c.author,c.created,c.content,c.status,p.title from comments as c left join posts as p on c.post_id=p.id order by c.id limit ? , ?"
        conn.query(sql,[offset,pageSize],(err,result)=>{
            if (err) return callback(err)
            callback(null, result)
        })
    },
    delCommentsInfoById(id,callback){ // 通过id删除对应的数据
        //写sql语句根据id删除对应数据
        var sql = "delete from comments where id = ?"
        conn.query(sql,[id],(err,result)=>{
            if(err) return callback(err)
            callback(null,true)
        })
    },
    updateCommentsStatusById(id, callback) {// 通过id更新对应的数据
        //写sql语句更新对应id的数据
        var sql = "update comments set status ='approved' where id = ?"
        conn.query(sql,[id],(err,result)=>{
            if (err) return callback(err)
            callback(null, true)
        })
    }
}